﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Mdv.wssAddins
{
    public class wssListTypeConverter : StringConverter
    {
        private static string[] types = new string[] { "100 Generic list", "101 Document library", "102 Survey", "103 Links list", "104 Announcements list",
                "105 Contacts list", "106 Events list", "107 Tasks list", "108 Discussion board", "109 Picture library", "110 Data sources",
                "111 Site template gallery", "112 User Information list", "113 Web Part gallery", "114 List template gallery", "115 XML Form library",
                "116 Master pages gallery", "117 No-Code Workflows", "118 Custom Workflow Process", "119 Wiki Page library", "120 Custom grid for a list",
                "130 Data Connection library", "140 Workflow History", "150 Gantt Tasks list", "200 Meeting Series list", "201 Meeting Agenda list",
                "202 Meeting Attendees list", "204 Meeting Decisions list", "207 Meeting Objectives list", "210 Meeting text box", "211 Meeting Things To Bring list",
                "212 Meeting Workspace Pages list", "301 Blog Posts list", "302 Blog Comments list", "303 Blog Categories list", "1100 Issue tracking",
                "1200 Administrator tasks list" };
        private static StandardValuesCollection defaultTypes = new StandardValuesCollection(types);
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return defaultTypes;
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                string retElement = String.Empty;
                if (types.Any<string>(delegate(string element)
                {
                    if (0 == String.Compare(element.Split(' ')[0], value.ToString()))
                    {
                        retElement = element;
                        return true;
                    }
                    else return false;
                }))
                    return retElement;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                string s = value.ToString().Split(' ')[0];
                int res;
                if (Int32.TryParse(s, out res)) return res;
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}